# -*- coding: utf-8 -*-
# @Author	: brotherbaby
# @Date		: 2021-04-28 15:17:02 
# @Last Modified by:   brotherbaby
# @Last Modified time: 2021-04-28 15:17:02
# Thanks for your comments!
# !/usr/bin/env python
# ! -*- coding: utf-8 -*-

import os

import yaml

from src.utils.singleton import singleton


def get_config_file_path():
    path_one = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
    path_two = os.path.dirname(path_one)
    path_three = os.path.dirname(path_two)
    file_name = path_three + "/etc/config.yaml"
    if os.path.exists(file_name):
        return file_name
    return "etc/config.yaml"


@singleton
class Config(object):
    """
    配置
    """

    api = {}
    bizdb = {}
    interfaces = {}
    app = {}

    def __init__(self, configDir):
        self.load(configDir)
        fpath, fname = os.path.split(configDir)
        self.load(os.path.join(fpath, 'local.' + fname))

    def load(self, configDir):
        if os.path.isfile(configDir):
            f = open(configDir)
            d = yaml.load(f, Loader=yaml.FullLoader)
            print(f"configurations: {d}")
            for k, v in d.items():
                if hasattr(self, k):
                    if isinstance(v, dict):
                        for _k, _v in v.items():
                            self.__getattribute__(k)[_k] = _v
                    else:
                        self.__setattr__(k, v)


file_name = get_config_file_path()
CONF = Config(file_name)

__all__ = ["CONF"]
